//
//  DetailOfDestinationViewController.m
//  Follow Me
//
//  Created by apple on 2017/5/13.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "DetailOfDestinationViewController.h"
#import "Destinations+CoreDataClass.h"
#import "Routes+CoreDataClass.h"
#import "RouteCell.h"
#import "DetailOfRouteViewController.h"
#import "MainModel.h"
@interface DetailOfDestinationViewController ()
@property (nonatomic,strong)NSArray *RouteCells;
@property (nonatomic,strong)NSArray *DestinationCells;
@property (nonatomic,strong) RouteCell *Rcell;
@end

@implementation DetailOfDestinationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.DestinationCells = [Destinations findByAttribute:@"destinationName" withValue:[MainModel model].destinatonMsg.destinationName];
    Destinations *destinatonWithSomeRoutes = [_DestinationCells firstObject];
    NSString *desNameOfRoute = destinatonWithSomeRoutes.destinationName;
    self.RouteCells = [Routes findByAttribute:@"desName" withValue:desNameOfRoute];
//    NSSet *routes1 =[MainModel model].destinatonMsg.route;
//    self.RouteCells = [routes1 allObjects];
    [self.tableView registerNib:[UINib nibWithNibName:@"RouteCell" bundle:nil] forCellReuseIdentifier:@"RouteCell"];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
    
    // Do any additional setup after loading the view.
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
    [self.navigationController.navigationBar setHidden:NO];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
    self.navigationController.navigationBar.frame = CGRectMake(0, 20, 375, 40);
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.RouteCells count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
        RouteCell *cell  = [tableView dequeueReusableCellWithIdentifier:@"RouteCell"forIndexPath:indexPath];
        [cell ConfigRouteCell:[self.RouteCells objectAtIndex:indexPath.row]];
        if (cell == nil){
            cell=[[[NSBundle mainBundle]loadNibNamed:@"RouteCell" owner:(self) options:nil]lastObject];
        }
        
        return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
           [MainModel model].routeMsg = self.RouteCells[indexPath.row];
        
        DetailOfRouteViewController *detailOfRouteVC = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailOfRoute"];
        [self.navigationController pushViewController:detailOfRouteVC animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
        return 270.0f;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
